package Datebase;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;


public class delete extends JFrame {
    public static void main(String[] args) {
        System.setProperty("sun.java2d.noddraw", "true");
        new  delete();
    }

    public  delete(){
        JFrame mod = new JFrame("学生信息删除界面");
        URL resource = delete.class.getClassLoader().getResource("kun.png");
        Image image = new ImageIcon(resource).getImage();
        mod.setIconImage(image);
        mod.setLayout(null);
        mod.setSize(400,200);
        mod.setLocationRelativeTo(null);

        JLabel tot1 = new JLabel("请输入要删除的学号:");
        tot1.setFont(new Font("宋体", Font.PLAIN, 15));
        tot1.setBounds(0,20,200,55);
        mod.add(tot1);

        JTextField textField1 = new JTextField();
        textField1.setFont(new Font("微软雅黑",Font.PLAIN,15));
        textField1.setColumns(10);
        textField1.setBounds(160,35,100,25);
        mod.add(textField1);

        JButton button= new JButton("删除");
        button.setFont(new Font("微软雅黑",Font.PLAIN,15));
        button.setBounds(270,35,75,25);
        mod.add(button);


        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
                    String user = "HR";
                    String password = "root";

                    Connection conn = DriverManager.getConnection(dbURL, user, password);
                    String sql = "select * from STUDENT where SNO = '" + textField1.getText() + "'";
                    PreparedStatement pstm = conn.prepareStatement(sql);
                    ResultSet rs = pstm.executeQuery();

                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }

                    if (count == 0) {
                        JOptionPane.showMessageDialog(null, "学号不存在",
                                "提示", JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        try {
                            Class.forName("oracle.jdbc.driver.OracleDriver");
                            String dbURL1 = "jdbc:oracle:thin:@localhost:1521:XE";
                            String user1 = "HR";
                            String password1 = "root";
                            Connection conn1 = DriverManager.getConnection(dbURL1, user1, password1);
                            String sql1 = "DELETE STUDENT WHERE SNO='"+textField1.getText()+"'";
                            PreparedStatement pstm1 = conn1.prepareStatement(sql1);
                            ResultSet rs1 = pstm1.executeQuery();

                            JOptionPane.showMessageDialog(null, "删除成功！该学生的成绩信息也删除了","状态提示", JOptionPane.INFORMATION_MESSAGE);

                        } catch (ClassNotFoundException cnfe) {
                            cnfe.printStackTrace();
                            JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                        } catch (SQLException sqle) {
                            sqle.printStackTrace();
                            JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                    }
                    catch (ClassNotFoundException cnfe) {
                        cnfe.printStackTrace();
                        JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
                    } catch (SQLException sqle) {
                        sqle.printStackTrace();
                        JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
                    }

            }
        });

        mod.repaint();
        mod.setVisible(true);
    }
}
